package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingCostAdjustmentEntity;
import com.ly.heart.domain.KingDocTypeEntity;
import com.ly.heart.domain.MaterialEntity;
import com.ly.heart.mapper.KingCostAdjustmentMapper;
import com.ly.heart.service.IKingCostAdjustmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.heart.service.IKingDocTypeService;
import com.ly.heart.service.IMaterialService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * <p>
 * 金蝶-成本调整单 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-07-30
 */
@Service
public class KingCostAdjustmentServiceImpl extends ServiceImpl<KingCostAdjustmentMapper, KingCostAdjustmentEntity> implements IKingCostAdjustmentService {

    @Resource
    private IKingDocTypeService iKingDocTypeService;
    @Resource
    private IMaterialService iMaterialService;

    @Override
    public KingCostAdjustmentEntity saveSingle ( KingCostAdjustmentEntity kingCostAdjustmentEntity ) {
        kingCostAdjustmentEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingCostAdjustmentEntity.setDocNo ( "CBTZD".concat ( String.format ( "%08d", count ) ) );
        kingCostAdjustmentEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingCostAdjustmentEntity.setCreateTime ( DateUtils.getNowDate () );
        kingCostAdjustmentEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        boolean save = this.save ( kingCostAdjustmentEntity );
        if ( ! save ) {
            throw new CustomException ( "新增成本调整单失败！" );
        }
        return kingCostAdjustmentEntity;
    }

    @Override
    public AjaxResult queryList ( KingCostAdjustmentEntity kingCostAdjustmentEntity ) {
        IPage<KingCostAdjustmentEntity> page = this.page ( new Page<> ( kingCostAdjustmentEntity.getPage (), kingCostAdjustmentEntity.getLimit () ) );
        page.getRecords ().forEach ( detail -> {
            if ( StringUtils.isNotBlank ( detail.getDocTypeCode () ) ) {
                KingDocTypeEntity docType = iKingDocTypeService.getOne ( new LambdaQueryWrapper<KingDocTypeEntity> ().
                        eq ( KingDocTypeEntity :: getDocCode, detail.getDocTypeCode () ) );
                detail.setDocTypeName ( docType.getTitle () );
            }
            if ( StringUtils.isNotBlank ( detail.getMaterialCode () ) ) {
                MaterialEntity materialEntity = iMaterialService.getOne ( new LambdaQueryWrapper<MaterialEntity> ().
                        eq ( MaterialEntity :: getMaterialCode, detail.getMaterialCode () ) );
                detail.setMaterialName ( materialEntity.getMaterialName () );
                detail.setModel ( materialEntity.getModel () );
            }
        } );
        return AjaxResult.success ( page );
    }
}
